<script setup lang="ts">
import { useRouter } from "vue-router";
import dayjs from "dayjs";
import { showFailToast, showImagePreview } from "vant";
import type { Message, Prescription } from "@/types/chitchat";
import type { Image } from "@/types/consultTypes";
import { IllnessTime, MsgType, PrescriptionStatus } from "@/enum";
import useUserStore from "@/pinia/modules/userStore.ts";
import useLookPre from "@/hooks/useLookPre.ts";
import EvaluateCard from "@/views/home/procedure/chitchat/components/evaluateCard/evaluateCard.vue";
import { getConsultFlagText, getIllnessTimeText } from "@/utils/index.ts";


withDefaults(defineProps<{
    message: Message
}>(), {});

const userStore = useUserStore();

const router = useRouter();


const formatTime = (time: string) => dayjs(time).format("HH:mm");

const loadSuccess = () => {
    window.scrollTo(0, document.body.scrollHeight);
};


/*点击查看病情图片的回调函数*/
const handlePreviewImage = (pictures: Image[]) => {
    if (pictures && pictures.length) {
        showImagePreview(pictures.map((item) => item.url));
    } else {
        showFailToast("没有上传病情图片");
    }
};

/*查看处方的回调函数*/
const handleLookPre = async (id: string) => {
    await useLookPre(id);
};


/*购买药品的回调函数*/
const buy = (prescription: Prescription) => {
    if (prescription.id) {
        if (prescription.status === PrescriptionStatus.Invalid) {
            showFailToast("处方失效");
        }
        if (prescription.status === PrescriptionStatus.NotPayment) {
            router.push({ path: "/medicine/pay", query: { id: prescription.id } });
        }
    }
};

</script>

<template>
    <!-- 患者卡片 -->
    <div class="msg msg-illness" v-if="message.msgType===MsgType.CardPat">
        <div class="patient van-hairline--bottom">
            <p>{{ message!.msg!.consultRecord!.patientInfo.name }} {{ message!.msg!.consultRecord!.patientInfo.genderValue }} {{ message!.msg!.consultRecord!.patientInfo.age }}</p>
            <p>{{ getIllnessTimeText(message!.msg!.consultRecord!.illnessTime as IllnessTime) }} | {{ getConsultFlagText(message!.msg!.consultRecord!.consultFlag) }}</p>
        </div>
        <van-row>
            <van-col span="6">病情描述</van-col>
            <van-col span="18">{{ message!.msg!.consultRecord!.illnessDesc }}</van-col>
            <van-col span="6">病情图片</van-col>
            <van-col span="18" @click="handlePreviewImage(message!.msg!.consultRecord!.pictures )">点击查看</van-col>
        </van-row>
    </div>
    <!-- 通用通知 -->
    <div class="msg msg-tip" v-if="message.msgType===MsgType.Notify">
        <div class="content">
            <span>{{ message.msg.content }}</span>
        </div>
    </div>
    <!-- 温馨提示 -->
    <div class="msg msg-tip" v-if="message.msgType === MsgType.NotifyTip">
        <div class="content">
            <span class="green">温馨提示：</span>
            <span>{{ message.msg.content }}</span>
        </div>
    </div>
    <!-- 我发的消息 -->
    <div class="msg msg-to" v-if="message.msgType === MsgType.MsgText && userStore.userInfo?.id === message.from">
        <div class="content">
            <div class="time">{{ formatTime(message.createTime) }}</div>
            <div class="pao">{{ message.msg.content }}</div>
        </div>
        <van-image :src="userStore.userInfo?.avatar" />
    </div>
    <!-- 医生发的消息 -->
    <div class="msg msg-from" v-if="message.msgType === MsgType.MsgText && userStore.userInfo?.id !== message.from">
        <van-image :src="message.fromAvatar" />
        <div class="content">
            <div class="time">{{ formatTime(message.createTime) }}</div>
            <div class="pao">{{ message.msg.content }}</div>
        </div>
    </div>
    <!--发送图片-->
    <div class="msg msg-to" v-if="message.msgType === MsgType.MsgImage && userStore.userInfo?.id === message.from">
        <div class="content">
            <div class="time">{{ formatTime(message.createTime) }}</div>
            <van-image fit="contain" :src="message.msg.picture?.url" @load="loadSuccess()" />
        </div>
        <van-image :src="userStore.userInfo?.avatar" />
    </div>
    <!--接收图片-->
    <div class="msg msg-from" v-if="message.msgType === MsgType.MsgImage && userStore.userInfo?.id !== message.from">
        <van-image :src="message.fromAvatar" />
        <div class="content">
            <div class="time">{{ formatTime(message.createTime) }}</div>
            <van-image fit="contain" :src="message.msg.picture?.url" @load="loadSuccess()" />
        </div>
    </div>
    <!-- 关闭问诊-->
    <div class="msg msg-tip msg-tip-cancel" v-if="message.msgType === MsgType.NotifyCancel">
        <div class="content">
            <span>{{ message.msg.content }}</span>
        </div>
    </div>
    <!--医生评价-->
    <div class="msg msg-comment" v-if="message.msgType === MsgType.CardEva || message.msgType === MsgType.CardEvaForm">
        <evaluate-card :evaluateDoc="message.msg.evaluateDoc"></evaluate-card>
    </div>
    <!-- 处方卡片 -->
    <div class="msg msg-recipe" v-if="message.msgType===MsgType.CardPre">
        <div class="content">
            <div class="head van-hairline--bottom">
                <div class="head-tit">
                    <h3>电子处方</h3>
                    <p @click="handleLookPre(message.msg!.prescription!.id)">原始处方
                        <van-icon name="arrow"></van-icon>
                    </p>
                </div>
                <p> {{ message.msg.prescription!.name }} {{ message.msg.prescription!.genderValue }} {{ message.msg.prescription!.age }}岁 {{ message.msg.prescription!.diagnosis }}</p>
                <p>开方时间:{{ message.msg.prescription!.createTime }}</p>
            </div>
            <div class="body">
                <div class="body-item" v-for="med in message.msg.prescription!.medicines" :key="med.id">
                    <div class="durg">
                        <p>{{ med.name }} {{ med.specs }}</p>
                        <p>{{ med.usageDosag }}</p>
                    </div>
                    <div class="num">x{{ med.quantity }}</div>
                </div>
            </div>
            <div class="foot" @click="buy(message.msg.prescription)"><span>购买药品</span></div>
        </div>

    </div>
</template>

<style scoped lang="scss">
@import "@/styles/chitchat";
</style>
